﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace XWG.JWBST.KckpHistoryProcess
{
    /// <summary>
    /// 快处快赔 历史记录查询
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            string info = reader.ReadToEnd();
            info = HttpUtility.UrlDecode(info);
            JObject oka = (JObject)JsonConvert.DeserializeObject(info);
            string Phone = oka["Phone"].ToString().Replace("\"", string.Empty);
            string LSH = oka["LSH"].ToString().Replace("\"", string.Empty);
           
            //如果流水号为空则通过手机号查询历史记录
            if (string.IsNullOrEmpty(LSH))
            {
                List<History> reHistory = new List<History>();
                List<XWG.JWBST.JWBSTEntity.TbBstFastprocess> fastList = new List<JWBSTEntity.TbBstFastprocess>();
                fastList = XWG.JWBST.JWBSTBLL.TbBstFastprocessBLL.GetInfoByPhone(Phone);
                if (fastList.Count == 0)
                {
                    return "暂无该手机号报案记录";
                }
                else
                {
                    for (int i = 0; i < fastList.Count; i++)
                    {
                        History his = new History();
                        his.Title = fastList[i].FastLsh;
                        his.KCKPAddress = fastList[i].FastAddress;
                        his.KCKPLPNa = fastList[i].FastLpna;
                        his.KCKPLPNb = fastList[i].FastLpnb;
                        his.KCKPPhonea = fastList[i].FastPhonea;
                        his.KCKPPhoneb = fastList[i].FastPhoneb;
                        his.KCKPAuditstatus = fastList[i].FastAuditstatus;
                        his.KCKPAuditresult = fastList[i].FastAuditresult;
                        his.KCKPUpTime = fastList[i].FastUptime.ToString();
                        if (fastList[i].FastAudittime == null)
                        {
                            his.KCKPAudittime = "尚未处理";
                        }
                        else
                        {
                            his.KCKPAudittime = fastList[i].FastAudittime.ToString();
                        }
                        reHistory.Add(his);
                    }

                    //记录请求信息
                    JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                    visit.Id = Guid.NewGuid().ToString();
                    visit.Phone = Phone;
                    visit.Time = DateTime.Now;
                    visit.Modulename = "Kckp";
                    visit.Optionname = "根据手机号查询快处快赔历史记录";
                    visit.Other = oka.ToString();
                    XWG.JWBST.JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);
                    // JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);
                    IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                    string json = JsonConvert.SerializeObject(reHistory, dtConverter);
                    return json;
                }
            }
            //流水号不为空则根据流水号查询该事故审核状态
            else
            {
                XWG.JWBST.JWBSTEntity.TbBstFastprocess fastinfo = new JWBSTEntity.TbBstFastprocess();
                History fasthis = new History();
                fastinfo = XWG.JWBST.JWBSTBLL.TbBstFastprocessBLL.GetAduitByLSH(LSH);
                if (fastinfo.FastAuditstatus == "已审核")
                {
                    fasthis.Title = fastinfo.FastLsh;
                    fasthis.KCKPAddress = fastinfo.FastAddress;
                    fasthis.KCKPLPNa = fastinfo.FastLpna;
                    fasthis.KCKPLPNb = fastinfo.FastLpnb;
                    fasthis.KCKPPhonea = fastinfo.FastPhonea;
                    fasthis.KCKPPhoneb = fastinfo.FastPhoneb;
                    fasthis.KCKPAuditstatus = fastinfo.FastAuditstatus;
                    fasthis.KCKPAuditresult = fastinfo.FastAuditresult;
                    fasthis.KCKPUpTime = fastinfo.FastUptime.ToString();
                    fasthis.KCKPAudittime = fastinfo.FastAudittime.ToString();

                    //记录请求信息
                    JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                    visit.Id = Guid.NewGuid().ToString();
                    visit.Phone = fasthis.KCKPPhonea;
                    visit.Time = DateTime.Now;
                    visit.Modulename = "KckpHistory";
                    visit.Optionname = "根据流水号查询快处快赔历史记录";
                    visit.Other = oka.ToString();
                    JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

                    IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                    string json = JsonConvert.SerializeObject(fasthis, dtConverter);
                    return json;
                }
                else
                {
                    return "0";
                }
            }
        }
    }

    public class History
    {
        public string Title { get; set; }
        public string KCKPPhonea { get; set; }
        public string KCKPPhoneb { get; set; }
        public string KCKPLPNa { get; set; }
        public string KCKPLPNb { get; set; }
        public string KCKPUpTime { get; set; }
        public string KCKPAddress { get; set; }
        public string KCKPAudittime { get; set; }
        public string KCKPAuditresult { get; set; }
        public string KCKPAuditstatus { get; set; }
    }

}
